#include <string.h>
#include <iostream>

#include "include/MissionStrategy.h"
#include "tinyxml/tinyxml.h"
#include "include/Mission_0.h"
#include "include/Mission_1.h"
#include "include/Mission_2.h"
#include "include/Mission_3.h"
#include "include/Mission_4.h"
#include "include/Mission_5.h"
#include "include/Mission_6.h"
#include "include/Mission_7.h"
#include "include/Mission_8.h"
#include "include/Mission_9.h"
#include "include/Mission_10.h"
#include "include/Mission_11.h"
#include "include/Mission_12.h"
#include "include/Mission_13.h"

#define MISSIONS_DEFAULT_FILE "missions.xml"

using namespace std;

MissionStrategy::MissionStrategy(const char * _description)
   : description(new char[strlen(_description)+1])
{
   strcpy(description, _description);
}

MissionStrategy::~MissionStrategy()
{
   delete []description;
}

MissionStrategy* MissionStrategy::createMission(int id)
{
   int cur_id;
   TiXmlDocument doc(MISSIONS_DEFAULT_FILE);
   if( !doc.LoadFile() )
   {
      cerr << "erreur lors du chargement de " << MISSIONS_DEFAULT_FILE << endl;
      cerr << "error #" << doc.ErrorId() << " : " << doc.ErrorDesc() << endl;
      exit(0);
   }

   TiXmlElement * current_node = doc.RootElement()->FirstChildElement();

   while( current_node->QueryIntAttribute("id", &cur_id) == TIXML_SUCCESS && cur_id != id)
      current_node = current_node->NextSiblingElement();
   
   //cout << current_node->FirstChildElement("description")->GetText() << endl;

   switch(id)
   {
      case 0:
         return new Mission_0( current_node->FirstChildElement("description")->GetText() );
      case 1:
         return new Mission_1( current_node->FirstChildElement("description")->GetText() );
      case 2:
         return new Mission_2( current_node->FirstChildElement("description")->GetText() );
      case 3:
         return new Mission_3( current_node->FirstChildElement("description")->GetText() );
      case 4:
         return new Mission_4( current_node->FirstChildElement("description")->GetText() );
      case 5:
         return new Mission_5( current_node->FirstChildElement("description")->GetText() );
      case 6:
         return new Mission_6( current_node->FirstChildElement("description")->GetText() );
      case 7:
         return new Mission_7( current_node->FirstChildElement("description")->GetText() );
      case 8:
         return new Mission_8( current_node->FirstChildElement("description")->GetText() );
      case 9:
         return new Mission_9( current_node->FirstChildElement("description")->GetText() );
      case 10:
         return new Mission_10( current_node->FirstChildElement("description")->GetText() );
      case 11:
         return new Mission_11( current_node->FirstChildElement("description")->GetText() );
      case 12:
         return new Mission_12( current_node->FirstChildElement("description")->GetText() );
      case 13:
         return new Mission_13( current_node->FirstChildElement("description")->GetText() );
      default:
         cerr << "L'id ("<<id<<")de la mission n'est pas defini !" << endl;
         return NULL;
   }
}
